package com.css.resoft.p2pmonitor.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.css.resoft.p2pmonitor.util.SAXParserHandle;

public class PutCleanData {
	public static String host = "10.254.110.70";
	public static int port = 9090;
	
	public static void main(String[] args) throws Exception {
		
		long startTime = System.currentTimeMillis();
		
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		SAXParserHandle handle = new SAXParserHandle(host,port);
		String fileDir = "D:"+File.separator+"p2p";
		String errFileDir = "D:"+File.separator+"err"+File.separator;
		String cleanFileDir = "D:"+File.separator+"clean";
		List<String> filePaths = new ArrayList<String>();
		
		int num = 0;
		
		getFileList(fileDir,filePaths);
		
		handle.openThrift();
		
		for(String file :filePaths){
			num++;
			System.out.print(num);
			parseZipFile(parser,handle,file,errFileDir);
//			moveZipFile(file,cleanFileDir);
		}
		handle.closeThrift();
		
		long endTime = System.currentTimeMillis();
		
		System.out.println(endTime-startTime);
	}
	
	private static void moveZipFile(String file, String cleanFileDir) {
		
		try {
			File startFile = new File(file);
			File tmpFile = new File(cleanFileDir);
			if(!tmpFile.exists()){
				tmpFile.mkdirs();
			}
			String fileName = startFile.getName();
			startFile.renameTo(new File(cleanFileDir+File.separator+fileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public static void getFileList(String fileDir, List<String> filePaths) {
		File file = new File(fileDir);
		if(file.isFile() && fileDir.endsWith("zip")){
			filePaths.add(fileDir);
		}else if(file.isDirectory()){
			for(String str: file.list()){
				getFileList(fileDir+File.separator+str,filePaths);
			}
		}
	}
	
	public static void parseZipFile(SAXParser parser,SAXParserHandle handle,String file,String errFileDir){
		try {
			System.out.println("------------------start deal zipfile: "+file+"-----------------------");
			ZipFile zf = new ZipFile(file);
			ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(file))); 
			ZipEntry ze;
			while((ze = zin.getNextEntry())!= null){
				if(!ze.isDirectory()){
					zin.closeEntry();
					long size = ze.getSize();
					if(size > 0){
						InputStream is = null;
						try {
							is = zf.getInputStream(ze);
							System.out.print(" parse ");
							parser.parse(is, handle);
							System.out.print(" put ");
							handle.putData();
						} catch (Exception e) {
							e.printStackTrace();
							e.printStackTrace(new PrintStream(new File(errFileDir + ze.getName()+"_log.txt")));
							is = zf.getInputStream(ze);
							FileOutputStream fos = new FileOutputStream(errFileDir+ze.getName());
							byte []bt =  new byte[1024];
							while(is.read(bt) != -1){
								fos.write(bt);
							}
							fos.flush();
							fos.close();
							
							System.out.print(ze.getName() + " failed prase");
						}finally{
							is.close();
						}
					}else{
						System.out.print(" xmlfile: "+ze.getName()+"size < 0 ");
					}
				}
			}
			
			zin.close();
			zf.close();
		}catch(Exception e){
			System.out.println("------------------------open zipfile: "+file+" failed---------------------------");
			e.printStackTrace();
		}
		System.out.println("------------------------end deal zipfile: "+file+"------------------------");
		System.out.println();
		
	}
	
}
